<?php
namespace Admin\Controller;
class MenuController extends BaseController{
    function index(){
        
       
       
        if(IS_AJAX){
            $data=$this->getMenus();
           // $data=M('menu')->select();
            $this->ajaxReturn($data);
        }
        
        $this->display();
    }
    
    function add() {
        if(IS_POST){
            $_POST['ctime']=time();
            $id=M('menu')->add(I('post.'));
            if($id){
                $this->redirect('index');
            }else{
                $this->error('添加失败',U(),5);
            }
        }
        $menus=M('menu')->getField('id,title');
        $actions=M('action')->getField('id,title');
        
        $this->assign(array('menus'=>$menus,'actions'=>$actions));
        $this->display();
    }
    
   protected function getMenus($pid=0,$data=array(),$l=0){
        $main=M('menu')->where(array('pid'=>$pid))
        ->join('tp_action on tp_menu.aid=tp_action.id','left')
        ->field('tp_menu.*,tp_action.url')
        ->select();
        foreach ($main as $v){
                $str='';
                for($i=0;$i<$l;$i++){
                    $str.='__';
                }
                $v['title']=$str.$v['title'];
                $data[]=$v;
                $data=$this->getMenus($v['id'],$data,$l+1);
           
        }
        return $data;
    }
    function test(){
        W('Menu/getMenu');
    }
}